package net.anzhuo.controller.db.syncdata;



import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.utils.DateUtil;
import net.anzhuo.common.utils.RandomGUID;
import net.anzhuo.entity.GsGonggao;
import net.anzhuo.entity.GsPriority;
import net.anzhuo.entity.db.syncdata.CourtCaseInfo;
import net.anzhuo.entity.db.syncdata.CourtCountResult;
import net.anzhuo.entity.db.syncdata.PageUtilsCourtCase;
import net.anzhuo.entity.db.syncdata.Paging;
import net.anzhuo.entity.db.syncdata.QueryGonggaoResult;
import net.anzhuo.entity.db.syncdata.Result;
import net.anzhuo.service.GsGonggaoService;
import net.anzhuo.service.GsPriorityService;

@RestController
@Slf4j
public class KeyNoStatusController {




	
	@Autowired
	private GsPriorityService gsPriorityService;
	
	@RequestMapping("/querykeynostatus")
	public net.anzhuo.entity.db.syncdata.Result querykeynostatus(String keyNo) {
		log.info("===>querykeynostatus="+keyNo);
		long startTime=System.currentTimeMillis();
		
		net.anzhuo.entity.db.syncdata.Result detailResult = new net.anzhuo.entity.db.syncdata.Result();
		detailResult.setStatus("200");
		detailResult.setMessage("Update success.");
		 long endTime=System.currentTimeMillis();
		 GsPriority gsPriority=new GsPriority();
		 if(keyNo==null||keyNo.equals(""))
		 {
			 return detailResult;
		 }
		 gsPriority.setKeyNo(keyNo);
		 List<GsPriority> list=gsPriorityService.getByKeyNo(gsPriority);
		 if(list.size()>0)
		 {
			 gsPriority=list.get(0);
			 long a=DateUtil.getDateTimeLong(DateUtil.getDate("yyyy-MM-dd HH:mm:ss"))-DateUtil.getDateTimeLong(gsPriority.getOperdate());
			 int del=(int)a/(24*60*60*1000);
			 log.info("已更新"+del+"天");
			 if(del<2&&gsPriority.getStatus().equals("1"))
			 {
					detailResult.setStatus("200");
					detailResult.setMessage("Update success.");
			 }else
			 {
				 gsPriority.setStatus("0");
				 gsPriorityService.update(gsPriority);
				 GsPriority gp=new GsPriority();
				 gp.setKeyNo(keyNo);
				boolean result=false;
				 while(!result)
					{
						List<GsPriority> listt=gsPriorityService.getByKeyNo(gp);
						for(GsPriority g:listt)
						{
							String status=g.getStatus();
							if(status.equals("1"))
							{
								result=true;
								detailResult.setStatus("200");
								detailResult.setMessage("Update success.");
							}
						}
					}
			 }
		 }else
		 {
			 gsPriority.setKeyNo(keyNo);
			 gsPriority.setStatus("0");
			 gsPriority.setOperdate(DateUtil.getDate("yyyy-MM-dd HH:mm:ss"));
			 gsPriorityService.save(gsPriority);
			 GsPriority gp=new GsPriority();
			 gp.setKeyNo(keyNo);
			 boolean result=false;
			 while(!result)
				{
					List<GsPriority> listt=gsPriorityService.getByKeyNo(gp);
					for(GsPriority g:listt)
					{
						String status=g.getStatus();
						if(status.equals("1"))
						{
							result=true;
							detailResult.setStatus("200");
							detailResult.setMessage("Update success.");
						}
					}
				}
		 }
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" querykeynostatus 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
	
	
}
